package nl.renedegroot.android.intervalbeeper;

import android.app.ListActivity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.net.Uri;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ListView;

public class RegisterSoundActivity extends ListActivity
{

   private static final String TAG = "RegisterSoundActivity";
   private static final int MENU_ITEM_MAIN =  Menu.FIRST;
   private SharedPreferences preferences;
   private SoundsAdapter mSoundsAdapter;

   /*
    * (non-Javadoc)
    * @see android.app.Activity#onCreate(android.os.Bundle)
    */
   @Override
   protected void onCreate( Bundle savedInstanceState )
   {
      super.onCreate( savedInstanceState );
      setContentView( R.layout.custom_sounds );

      Intent intent = this.getIntent();
      Bundle extras = intent.getExtras();
      if( extras != null )
      {
         Uri streamUri = (Uri) extras.getParcelable( Intent.EXTRA_STREAM );
         String filename = extras.getString( Intent.EXTRA_SUBJECT );
         
         preferences = PreferenceManager.getDefaultSharedPreferences( this );
         Editor editor = preferences.edit();
         editor.putString( filename, streamUri.toString() );
         editor.commit();
      }

      mSoundsAdapter = new SoundsAdapter( this );
      setListAdapter( mSoundsAdapter );
   }
   
   @Override
   protected void onListItemClick(ListView l, View v, int position, long id) {
      super.onListItemClick(l, v, position, id);
      mSoundsAdapter.removeOne( position );
   }
   

   /*
    * (non-Javadoc)
    * @see android.app.Activity#onCreateOptionsMenu(android.view.Menu)
    */
   @Override
   public boolean onCreateOptionsMenu( Menu menu )
   {
      super.onCreateOptionsMenu( menu );

      menu.add( ContextMenu.NONE, MENU_ITEM_MAIN, ContextMenu.NONE, R.string.menu_main ).setIcon( android.R.drawable.ic_menu_agenda );
      return true;
   }

   /*
    * (non-Javadoc)
    * @see android.app.Activity#onOptionsItemSelected(android.view.MenuItem)
    */
   @Override
   public boolean onOptionsItemSelected( MenuItem item )
   {
      switch (item.getItemId())
      {
         case MENU_ITEM_MAIN:
            showBeeperDialog();
            return true;
         default:
            // Whoops, unknown menu item.
            Log.e( TAG, "Unknown menu item" );
      }
      return super.onOptionsItemSelected( item );
   }

   private void showBeeperDialog()
   {
      Intent intent = new Intent( this, BeeperActivity.class );
      startActivity( intent );
   }
}
